package com.bajiao.admin.controller.banner;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bajiao.admin.base.AdminBaseController;
import com.bajiao.common.api.vo.Result;
import com.bajiao.entity.banner.Banner;
import com.bajiao.service.banner.BannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 轮播图
 * @Author: FanJq
 * @Date: 2024-01-10
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "轮播图")
@RestController
@RequestMapping("/banner")
public class BannerController extends AdminBaseController<Banner, BannerService> {

    @Resource
    private BannerService bannerService;

    @ApiOperation(value = "轮播图-分页列表查询", notes = "轮播图-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList() {
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Banner::getDelFlag, 0).orderByAsc(Banner::getSort);
        startPage();
        return Result.ok(getDataTable(bannerService.list(queryWrapper)));
    }

    @ApiOperation(value = "轮播图-添加", notes = "轮播图-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody Banner banner) {
        bannerService.save(banner);
        return Result.ok("添加成功！");
    }

    @ApiOperation(value = "轮播图-编辑", notes = "轮播图-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody Banner banner) {
        bannerService.updateById(banner);
        return Result.ok("编辑成功!");
    }

    @ApiOperation(value = "轮播图-通过id删除", notes = "轮播图-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) Integer id) {
        bannerService.updateById(new Banner().setId(id).setDelFlag(1));
        return Result.ok("删除成功!");
    }

    @ApiOperation(value = "轮播图-批量删除", notes = "轮播图-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.bannerService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }
}
